<?php

namespace App\Exceptions;

use Exception;
use Illuminate\Auth\Access\AuthorizationException;
use Illuminate\Database\Eloquent\ModelNotFoundException;
use Illuminate\Validation\ValidationException;
use Laravel\Lumen\Exceptions\Handler as ExceptionHandler;
use Symfony\Component\HttpKernel\Exception\HttpException;
use Throwable;

class Handler extends ExceptionHandler
{
    /**
     * A list of the exception types that should not be reported.
     *
     * @var array
     */
    protected $dontReport = [
        AuthorizationException::class,
        HttpException::class,
        ModelNotFoundException::class,
        ValidationException::class,
        'Symfony\Component\HttpKernel\Exception\HttpException'
    ];

    /**
     * Report or log an exception.
     *
     * This is a great spot to send exceptions to Sentry, Bugsnag, etc.
     *
     * @param \Throwable $exception
     * @return void
     *
     * @throws \Exception
     */
    public function report(Throwable $exception)
    {
        parent::report($exception);
    }

    /**
     * Render an exception into an HTTP response.
     *
     * @param \Illuminate\Http\Request $request
     * @param \Throwable $exception
     * @return \Illuminate\Http\Response|\Illuminate\Http\JsonResponse
     *
     * @throws \Throwable
     */
    public function render($request, Throwable $exception)
    {
//        if (env('APP_DEBUG') and $this->isHttpException($exception)) {
//            // var_dump($exception->getMessage(), 5555555555555);
////             return $this->renderHttpException($exception);
//        }

        $message_details = '';
        if (env('APP_DEBUG')) {
            $message_details = $exception->__toString();
            return response()->json(array('code' => -400, 'msg' => $exception->getMessage(), 'details' => $message_details));
//            return parent::render($request, $exception);
//
        }
//        return response()->json(array('code' => -400, 'msg' => $exception->getMessage(), 'details' => $message_details));
//        return response()->json(array('code' => -400, 'msg' => '访问出错,请联系管理员', 'details' => '访问出错,请联系管理员'));
    }

    /**
     * Render the given HttpException.
     *
     * @param SymfonyComponentHttpKernelExceptionHttpException $e
     * @return SymfonyComponentHttpFoundationResponse
     */
    protected function renderHttpException(HttpException $e)
    {
        $status = $e->getStatusCode();
        // var_dump($status);
        if (view()->exists("errors.{$status}")) {
            return response(view("errors.{$status}", []), $status);
        } else {
            return response()->json(array('code' => -400, 'msg' => '访问出错,请联系管理员', 'details' => '访问出错,请联系管理员'));
//            return $status;
//            return (new SymfonyExceptionHandler(env('APP_DEBUG', false)))->createResponse($e);
        }
    }

    /**
     * Determine if the given exception is an HTTP exception.
     *
     * @param Exception $e
     * @return bool
     */
    protected function isHttpException(Throwable $e)
    {
        return $e instanceof HttpException;
    }
}
